/**
 * Copyright(c)2013 Beijing PeaceMap Co.,Ltd.
 * All right reserved. 
 */
package com.pmc.dwa.common.custom;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

/**
 * @description java.sql.Date类型日期转换器
 * @author aokunsang
 * @date 2013-6-18
 */
public class CustomDateConverter implements Converter<String, Date> {

	private String dateFormatPattern;  //转换的格式

    public CustomDateConverter(String dateFormatPattern) {
            this.dateFormatPattern = dateFormatPattern;
    }
	
	@Override
	public Date convert(String source) {
		 if(!StringUtils.hasLength(source)) {
             return null;
		 }
	     DateFormat df = new SimpleDateFormat(dateFormatPattern);
	     java.util.Date date = null;
	     try {
	    	 date = df.parse(source);
	    	 return new Date(date.getTime());
	     } catch (ParseException e) {
	         throw new IllegalArgumentException(String.format("类型转换失败，需要格式%s，但格式是[%s]", dateFormatPattern, source)); 
	     }
	}
}
